#coding:utf-8
from qiniu import Auth
from flask_restful import Api, Resource

class BuildToken(Resource):
    def get(self, key):
        # 需要填写你的 Access Key 和 Secret Key
        access_key = 'a1iysG0PUwVwBFpo9pxfAn4N3Wg4soDMDwVPV-xw'
        secret_key = 'v27Z8zyVd79UfK8KFFJdJkZsyGUyb-ofvRwAmbUz'
        # 构建鉴权对象
        q = Auth(access_key, secret_key)
        # 要上传的空间
        bucket_name = 'dynamic-image'
        # 上传后保存的文件名
        # key = 'x.jpg'

        # 生成上传 Token，可以指定过期时间等

        # 上传策略示例
        # https://developer.qiniu.com/kodo/manual/1206/put-policy
        policy = {
            # 'callbackUrl':'https://requestb.in/1c7q2d31',
            # 'callbackBody':'filename=$(fname)&filesize=$(fsize)'
            # 'persistentOps':'imageView2/1/w/200/h/200'
        }
        # 3600为token过期时间，秒为单位。3600等于一小时
        token = q.upload_token(bucket_name, key, 3600, policy)
        return token

class BuildTokenV1(Resource):
    def get(self):
        # 需要填写你的 Access Key 和 Secret Key
        access_key = 'a1iysG0PUwVwBFpo9pxfAn4N3Wg4soDMDwVPV-xw'
        secret_key = 'v27Z8zyVd79UfK8KFFJdJkZsyGUyb-ofvRwAmbUz'
        # 构建鉴权对象
        q = Auth(access_key, secret_key)
        # 要上传的空间
        bucket_name = 'dynamic-image'
        # 上传后保存的文件名
        # key = 'x.jpg'

        # 生成上传 Token，可以指定过期时间等

        # 上传策略示例
        # https://developer.qiniu.com/kodo/manual/1206/put-policy
        policy = {
            # 'callbackUrl':'https://requestb.in/1c7q2d31',
            # 'callbackBody':'filename=$(fname)&filesize=$(fsize)'
            # 'persistentOps':'imageView2/1/w/200/h/200'
        }
        # 3600为token过期时间，秒为单位。3600等于一小时
        token = q.upload_token(bucket_name, None, 3600, policy)
        return token